import os
import re
import shutil

INT_TYPE = "int"
STRING_TYPE = "varchar(45)"


def getModel(filePath):
    dictModel = {}
    fileObj = open(filePath, "r")
    line = fileObj.readline()
    while (line):
        line = fileObj.readline()
        if re.search("CREATE TABLE", line):
            splitStr = line.split(" ")
            tableName = splitStr[2].replace('`', "")
            content = ""
            while (True):
                line = fileObj.readline()
                if re.search("ENGINE=InnoDB", line):
                    break
                else:
                    line = line.strip(" ")
                    splitValue = line.split(' ')
                    if re.search("`", splitValue[0]):
                        content += (splitValue[0].replace('`', "")) + "-" + splitValue[1]
                        content += "|"
            content = content.strip("|")
            dictModel[tableName] = content

    return dictModel


def genModelClass(dictModel):
    content = '''
    class className
{
public:
	className() {}
	~className() {}

defineAttrs

	std::string operator[](const int& index)
	{
		switch (index)
		{
		operatorContent
		default:
			return "";
		}
	}
	
	void set(const int& index, char* value)
	{
		switch (index)
		{
		setContent
		default:
			break;
		}
	}

	std::string getDataType(int index)
	{
		switch (index)
		{
		dataTypeContent
		default:
			return "";
		}
	}

	std::string getAttributeName(const int& index)
	{
		switch (index)
		{
		attributeNameContent
		default:
			return "";
		}
	}

	bool isValid(int index)
	{
		switch (index)
		{
		validContent
		default:
			return false;
		}
	}

	std::string getName()
	{
		return "tableName";
	}

	int getAttributeNums()
	{
		return attributeNums;
	}
};\n\n
    '''
    fileObj = open("Model.h", "w")
    fileObj.write('''#pragma once\n#include<string>\n#include<typeinfo>\n#include<sstream>\n\n
    namespace DBModel
{
            ''')
    for className in dictModel.keys():
        values = dictModel[className]
        values = values.split("|")
        theContent = "" + content
        index = 1
        defineAttrs = ""
        operatorContent = ""
        setContent = ""
        dataTypeContent = ""
        attributeNameContent = ""
        validContent = ""
        tableName = className
        attributeNums = index
        for value in values:
            attributeNums = index
            ids = value.split("-")
            id = ids[0]
            idType = ids[1]
            if idType == INT_TYPE:
                defineAttrs += "int " + id + "=0;\n"
                operatorContent += "case " + str(index) + ":\n" + "  return std::to_string(" + id + ");\n"
                setContent += "case " + str(index) + ":\n    " + id + "= std::atoi(value);\n"
                dataTypeContent += "case " + str(index) + ":\n" + "  return typeid(" + id + ").name();\n"
                attributeNameContent += "case " + str(index) + ":\n" + "   return \"" + id + "\";\n"
                validContent += "case " + str(index) + ":\n" + "   return " + id + ">0;\n"
            elif idType == STRING_TYPE:
                defineAttrs += "std::string " + id + "=\"\";\n"
                operatorContent += "case " + str(index) + ":\n" + "  return " + id + ";\n"
                setContent += "case " + str(index) + ":\n    " + id + "=value;\n"
                dataTypeContent += "case " + str(index) + ":\n" + "  return typeid(" + id + ").name();\n"
                attributeNameContent += "case " + str(index) + ":\n" + "   return \"" + id + "\";\n"
                validContent += "case " + str(index) + ":\n" + "   return " + id + "!=\"\";\n"
            index += 1

        theContent = theContent.replace("className", className.capitalize())
        theContent = theContent.replace("defineAttrs", defineAttrs)
        theContent = theContent.replace("operatorContent", operatorContent)
        theContent = theContent.replace("setContent", setContent)
        theContent = theContent.replace("dataTypeContent", dataTypeContent)
        theContent = theContent.replace("attributeNameContent", attributeNameContent)
        theContent = theContent.replace("validContent", validContent)
        theContent = theContent.replace("tableName", tableName)
        theContent = theContent.replace("attributeNums", str(attributeNums))

        print(theContent)
        fileObj.write(theContent)
    fileObj.write("\n}")
    fileObj.close()


if (__name__ == "__main__"):
    dictModel = getModel("mmorpg.sql")
    genModelClass(dictModel)

    shutil.copy("Model.h", "../GameServer/GameServer/dbconnect/Model.h")
    os.system("pause")
    pass
